package com.wms.wstatus.service;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.wms.common.db.AbstractBaseService;
import com.wms.db.bean.StatusPerson;
import com.wms.db.bean.User;
import com.wms.db.dao.IStatusManagerDao;
import com.wms.db.dao.IStatusPersonDao;
import com.wms.db.dao.IStatusTeamDao;
import com.wms.db.dao.IUserDao;

public class WStatusService extends AbstractBaseService{
	Logger logger = Logger.getLogger(this.getClass());
	
	private IUserDao userDao;
	
	private IStatusPersonDao statusPersonDao;
	
	private IStatusTeamDao statusTeamDao;
	
	private IStatusManagerDao statusManagerDao;
	
	public void savePStatus(List<StatusPerson> ps) throws Exception{
		Date d = ps.get(0).getTime();
		User u = ps.get(0).getUser();
		List<StatusPerson> old = getPStatusByNameAndDate(u.getUsername(), d);
		if(null!=old && 0!=old.size()){
			statusPersonDao.deleteAll(old);
		}
		statusPersonDao.saveOrUpdateAll(ps);
	}
	
	public List<StatusPerson> getPStatusByName(String name) throws Exception{
		DetachedCriteria criteria = statusPersonDao.createDetachedCriteria();
		criteria.add(Restrictions.eq("user", new User(name)));
		return statusPersonDao.findByCriteria(criteria);
	}
	
	public List<StatusPerson> getPStatusByNameAndDate(String name, Date date) throws Exception{
		DetachedCriteria criteria = statusPersonDao.createDetachedCriteria();
		criteria.add(Restrictions.eq("user", new User(name)));
		criteria.add(Restrictions.eq("time", date));
		return statusPersonDao.findByCriteria(criteria);
	}
	
	public void setStatusPersonDao(IStatusPersonDao statusPersonDao) {
		this.statusPersonDao = statusPersonDao;
	}

	public IStatusPersonDao getStatusPersonDao() {
		return statusPersonDao;
	}

	public void setStatusTeamDao(IStatusTeamDao statusTeamDao) {
		this.statusTeamDao = statusTeamDao;
	}

	public IStatusTeamDao getStatusTeamDao() {
		return statusTeamDao;
	}

	public void setStatusManagerDao(IStatusManagerDao statusManagerDao) {
		this.statusManagerDao = statusManagerDao;
	}

	public IStatusManagerDao getStatusManagerDao() {
		return statusManagerDao;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public IUserDao getUserDao() {
		return userDao;
	}


}
